<?php
/*
 * @Description  : Token验证中间件
 * @Author       : https://github.com/skyselang
 * @Date         : 2020-05-05
 * @LastEditTime : 2020-12-24
 */

namespace app\wx\middleware;

use Closure;
use think\facade\Request;
use think\facade\Config;
use app\wx\service\AdminTokenService;

class AdminTokenVerify
{
    /**
     * 处理请求
     *
     * @param Request $request
     * @param Closure $next
     * @return Response
     */
    public function handle($request, Closure $next)
    {
        $menu_url       = Request::pathinfo();
        $api_white_list = Config::get('admin.api_white_list');

        if (!in_array($menu_url, $api_white_list)) {
            $admin_token = Request::header(Config::get('admin.heder_token_key'), '');
            if (empty($admin_token)) {
                exception('缺少请求头：token参数');
            }
            $admin_id = Request::header(Config::get('admin.heder_id_key'), '');
            if (empty($admin_id)) {
                exception('缺少请求头：admin_id参数');
            }
            AdminTokenService::verify($admin_token, $admin_id);
        }
        return $next($request);
    }
}
